import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:yinshui_caiying/widgets/picker/models/pickerItem_model.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

typedef SingleCallback(PickerItemModel data);

class SinglePickerContentView extends StatefulWidget {
  SinglePickerContentView(
      {required this.data,
      this.selectData,
      required this.onConfirm,
      this.title,
      this.showTitleBar = true});

  final List<PickerItemModel> data;
  final dynamic selectData;
  SingleCallback onConfirm;
  String? title;
  bool showTitleBar;
  @override
  State<StatefulWidget> createState() => SinglePickerState(this.data,
      this.selectData, this.onConfirm, this.showTitleBar, this.title);
}

class SinglePickerState extends State<SinglePickerContentView> {
  dynamic _selectData;
  List<PickerItemModel> _data = [];
  int _selectIndex = 0;
  SingleCallback _onConfirm;
  bool _showTitleBar;
  late FixedExtentScrollController scrollCtrl;
  String? _title;
  SinglePickerState(this._data, this._selectData, this._onConfirm,
      this._showTitleBar, this._title) {
    _init();
  }

  @override
  void dispose() {
    scrollCtrl.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.end,
      children: [_renderPickerView()],
    );
  }

  _init() {
    int pindex = 0;
    pindex = _data.indexWhere((element) => element.value == _selectData.value);
    if (_data.length != 0) {
      // 如果没有匹配到选择器对应数据，我们得修改选择器选中数据 ，不然confirm 返回的事设置的数据
      if (pindex < 0) {
        _selectData = _data[0].value;
        pindex = 0;
      }
      _selectIndex = pindex;
      scrollCtrl = new FixedExtentScrollController(initialItem: pindex);
    }
  }

  void _setPicker(int index) {
    var selectedProvince = _data[index].value;

    if (_selectData.toString() != selectedProvince.toString()) {
      setState(() {
        _selectIndex = index;
        _selectData = selectedProvince;
      });

      _notifyLocationChanged();
    }
  }

  void _notifyLocationChanged() {
    // if (widget.route.onChanged != null) {
    //   widget.route.onChanged(_selectData);
    // }
  }

  double _pickerLaberPadding(String text) {
    double left = 60;

    if (text != null) {
      left = left + text.length * 12;
    }
    return left;
  }

  double _pickerFontSize(String text) {
    // if (text == null || text.length <= 6) {
    //   return 18.0;
    // } else if (text.length < 9) {
    //   return 16.0;
    // } else if (text.length < 13) {
    //   return 12.0;
    // } else {
    //   return 10.0;
    // }
    return 50.sp;
  }

  Widget _renderPickerView() {
    Widget itemView = _renderItemView();

    if (!_showTitleBar) {
      return itemView;
    }
    List<Widget> viewList = <Widget>[];
    if (_showTitleBar) {
      viewList.add(_titleView());
    }
    // if (_pickerStyle!.menu != null) {
    //   viewList.add(_pickerStyle!.menu);
    // }
    viewList.add(itemView);

    return Column(children: viewList);
  }

  Widget _renderItemView() {
    // 选择器
    Widget cPicker = CupertinoPicker.builder(
      scrollController: scrollCtrl,
      itemExtent: 100.h,
      onSelectedItemChanged: (int index) {
        _setPicker(index);
      },
      childCount: _data.length,
      itemBuilder: (_, index) {
        String text = _data[index].name.toString();
        return Align(
            alignment: Alignment.center,
            child: Text(text,
                style: TextStyle(
                    color: Colors.black87, fontSize: _pickerFontSize(text)),
                textAlign: TextAlign.start));
      },
    );
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 40.h),
      height: 500.h,
      color: Colors.white,
      child: cPicker,
    );
  }

  // 选择器上面的view
  Widget _titleView() {
    return Container(
      height: 44.0,
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(30.w), topRight: Radius.circular(30.w))),
      child: Row(
        // mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          /// 取消按钮
          InkWell(
              onTap: () {
                Get.back();
              },
              child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.only(
                  left: 30.w,
                ),
                child: Text('取消',
                    style: TextStyle(color: Colors.black, fontSize: 45.sp)),
              )),

          /// 标题
          Expanded(
              child: Text(
            (_title != null ? _title : '')!,
            textAlign: TextAlign.center,
            style: TextStyle(fontSize: 45.sp),
          )),

          /// 确认按钮
          InkWell(
              onTap: () {
                _onConfirm(_data[_selectIndex]);
                Get.back();
              },
              child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.only(right: 30.w),
                child: Text('确认',
                    style: TextStyle(color: Colors.black, fontSize: 45.sp)),
              ))
        ],
      ),
    );
  }
}
